package com.pic.mgt.haishin.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.haishin.action.BusinessListMgtAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class BusinessListMgtCommandPerformer extends AbstractCommandPerformer {
	
	private final String commandName = "bizmgt";
	private final String commandDescritpion = "business list management.";
	
	private BusinessListMgtAction businessListMgtAction;
	
	@Override
	protected void addOption(Options options) {
		options.addOption("add", false, "add a business. (req: bn, bt)");
		options.addOption("rmv", false, "remove a business. (req: bn)");
		options.addOption("lis", false, "list effective businesses.");
		options.addOption("bn", true, "assign business name.");
		options.addOption("bt", true, "assign business type.");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("add")) {
			if (!commandLine.hasOption("bn") ||
				!commandLine.hasOption("bt")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name or business type.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			// -------------------------------------------
			
			boolean isAddBusinessSuccessful =
				this.businessListMgtAction.addBusiness(
									commandLine.getOptionValue("bn"),
									commandLine.getOptionValue("bt"));
			
			if (isAddBusinessSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("rmv")) {
			if (!commandLine.hasOption("bn")) {
					return
					super.getReplyMessageWithReplyCode(
							"did not provide business name.",
							this.COMMAND_PERFORM_FAILED_CODE);
				}
			
			// -------------------------------------------
			
			boolean isRemoveBusinessSuccessful =
				this.businessListMgtAction.removeBusiness(commandLine.getOptionValue("bn"));
			
			if (isRemoveBusinessSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("lis")) {
			return
				this.businessListMgtAction.getEffectiveBusinessListInfoString();
		}
		else {
			return
			super.getReplyMessageWithReplyCode(
					"did not specify management action (option: add, rmv, or lis).",
					this.COMMAND_PERFORM_FAILED_CODE);
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}
	
	// --- getter and setter ---

	public void setBusinessListMgtAction(BusinessListMgtAction businessListMgtAction) {
		this.businessListMgtAction = businessListMgtAction;
	}

}
